import { request } from '../utils/request'
import { decryptAES } from '../utils/aes'

async function addOrUpdateGroup(type, members, isTemp = 1) {
  const { data } = await request.post('/group/addOrUpdateGroup', { groupType: type, groupMember: members, isTemp })
  return data
}

async function getUserInfobyId(userId) {
  // const { data } = await request.get('/user/getInfo', { params: { userId } })  //不加密
  const { data } = await request.get('/user/getInfoEncrypt', { params: { userId } }) // 加密
  data.data = JSON.parse(decryptAES(data.data))
  return data
}

async function getUserInfobyPage(page = null, size = null, orgIds = null) {
  if (orgIds) {
    orgIds = orgIds.toString()
  }
  var data = {}
  if (orgIds === '0') {
    data = await request.get('/user/getPageInfos', { params: { page, size } })
  } else {
    data = await request.get('/user/getPageInfos', { params: { page, size, orgIds } })
  }
  return data.data
}

async function getDeviceInfobyId(deviceId) {
  const { data } = await request.get('/device/getInfo', { params: { deviceId } })
  return data
}

async function getDeviceInfobyPage(page = null, size = null, deviceType = null, orgIds = null) {
  if (orgIds) {
    orgIds = orgIds.toString()
  }
  var data = {}
  if (orgIds === '0') {
    data = await request.get('/device/getPageInfos', { params: { page, size, deviceType } })
  } else {
    data = await request.get('/device/getPageInfos', { params: { page, size, deviceType, orgIds } })
  }
  return data.data
}

async function getGroupInfobyId(groupId) {
  const { data } = await request.get('/group/getInfo', { params: { groupId } })
  return data
}

async function getUserGroupInfobyPage(page = null, size = null, isTemp = 0) {
  const { data } = await request.get('/group/getPageInfos', { params: { page, size, isTemp, groupType: 1 } })
  return data
}

async function getDeviceGroupInfobyPage(page = null, size = null, isTemp = 0) {
  const { data } = await request.get('/group/getPageInfos', { params: { page, size, isTemp, groupType: 2 } })
  return data
}

export { addOrUpdateGroup, getUserInfobyId, getUserInfobyPage, getDeviceInfobyId, getDeviceInfobyPage, getGroupInfobyId, getUserGroupInfobyPage, getDeviceGroupInfobyPage }
